package com.example.mybatisplus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.config.Result;
import com.example.mybatisplus.model.param.ConfigParam;
import com.example.mybatisplus.model.query.ConfigQuery;
import com.example.mybatisplus.model.result.ConfigResult;
import com.example.mybatisplus.service.ConfigService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统参数(Config)表控制层
 *
 * @author 宋义州
 * @since 2023-09-02 09:21:44
 */
@Tag(name = "系统参数管理")
@RestController
@RequestMapping("/api/config")
public class ConfigController {
    /**
     * 服务对象
     */
    @Resource
    private ConfigService configService;

    /**
     * 新增数据
     *
     * @param config 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "新增__系统参数__", description = "新增一个__系统参数__", security = {@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)})
    public Result<Void> insert(@Parameter(required = true) @RequestBody ConfigParam config) {
        this.configService.insertSelective(config);
        return Result.success();
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "删除__系统参数__", description = "根据主键删除__系统参数__", security = {@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)})
    public Result<Void> delete(@Parameter(description = "主键", required = true) @PathVariable Long id) {
        this.configService.deleteByPrimaryKey(id);
        return Result.success();
    }

    /**
     * 修改数据
     *
     * @param config 实体对象
     * @return 更新结果
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新__系统参数__", description = "更新一个__系统参数__", security = {@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)})
    @Parameter(name = "config", description = "Config", required = true, schema = @Schema(implementation = ConfigParam.class))
    public Result<Void> update(@Parameter(required = true) @RequestBody ConfigParam config) {
        this.configService.updateByPrimaryKeySelective(config);
        return Result.success();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/detail/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "获取__系统参数__", description = "根据id获取一个__系统参数__", security = {@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)})
    @Parameter(name = "id", description = "主键", required = true, schema = @Schema(implementation = Long.class))
    public Result<ConfigResult> detail(@Parameter(description = "主键", required = true) @PathVariable Long id) {
        return Result.success(this.configService.selectByPrimaryKey(id));
    }

    /**
     * 分页查询所有数据
     *
     * @param config 查询实体
     * @return 分页数据
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "分页获取__系统参数__", description = "分页获取__系统参数__列表", security = {@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)})
    @Parameters(value = {
            @Parameter(name = "pageNum", description = "页数", required = true, schema = @Schema(implementation = Integer.class)),
            @Parameter(name = "pageSize", description = "页条数", required = true, schema = @Schema(implementation = Integer.class))
    })
    public Result<Page<ConfigResult>> list(@Parameter(hidden = true) ConfigQuery config) {
        return Result.success(this.configService.selectByPage(config));
    }
}

